package com.lixin.web.messageCodec;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.lixin.utils.JsonUtils;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import org.springframework.stereotype.Component;

/**
 * Description:
 * Copyright:   Copyright (c)2023
 * Company:     sci
 *
 * @author: 张李鑫
 * @version: 1.0
 * Create at:   2023-07-26 23:57:33
 * <p>
 * Modification History:
 * Date         Author      Version     Description
 * ------------------------------------------------------------------
 * 2023-07-26     张李鑫                     1.0         1.0 Version
 */
public class JsonMessageEncode extends MessageToByteEncoder<Object> {
    private final ObjectMapper objectMapper= JsonUtils.MAPPER;
    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, Object msg, ByteBuf out) throws Exception {
        String json = objectMapper.writeValueAsString(msg);
        // 将 JSON 字符串写入到 ByteBuf 中
        out.writeBytes(json.getBytes());
    }
}
